#include "include.h"

// obliczenie "wag" dla poszczegolnych elmeruff
void prepare(int elmer_count) {
     int i, j;
     // dla kazdego elmera
     for(i=0; i<elmer_count; i++) {
         // sprawdz, czy ma nastepnik, a jesli tak, to ile i w ktorej ..
         for(j=0; j<elmer_count; j++) {
             if(i == j) continue;
             // jezeli mozna dopasowac bezposrednio po nim:
             if(!match(data[i].seq, data[j].seq)) {
                 data[i].next_count++;
                 // dodajemy 10 punktow za bezposrednie dopasowanie
                 data[i].weight -= 10;
             } else if(!match8(data[i].seq, data[j].seq)) {
                 data[i].next_count++;
                 data[i].weight -= 8;
             } else if(!match7(data[i].seq, data[j].seq)) {
                 data[i].next_count++;
                 data[i].weight -= 5;
             } else if(!match6(data[i].seq, data[j].seq)) {
                 data[i].next_count++;
                 data[i].weight -= 3;
             }
         }
     }
}

// generowanie rozwiazania
int solve(int elmer_count) {
      char *result = malloc((ELMER_LEN + elmer_count -1) * sizeof(char));
      
      int i, j, curr, next_count=0, prev_count=elmer_count;
      curr=0;
      for(i=0; i<elmer_count; i++) {
          for(j=0; j<elmer_count; j++) {
              if(match(data[curr].seq, data[j].seq) == 0 && data[j].used < TRESHOLD && curr != j) {
                  next[next_count++] = data[j].seq[ELMER_LEN-1];
                  data[j].used++;
                  curr=j;
                  break;
              }
          }
      }
      // -----------------------------------------------
      curr=0;
      for(i=0; i<elmer_count; i++) {
          for(j=0; j<elmer_count; j++) {
              if(match(data[j].seq, data[curr].seq) == 0 && data[j].used < TRESHOLD && curr != j) {
                  prev[--prev_count] = data[j].seq[0];
                  data[j].used++;
                  curr=j;
                  break;
              }
          }
      }
      int sum=0;
      for(i=0;i<elmer_count;i++) if(data[i].used > 0) sum++;
      
      //printf("%d\n", sum);
      //printf("prev=%d next=%d %f\n\n", elmer_count-prev_count, next_count, 100.0*sum/(EXPECTED_LEN-ELMER_LEN+1));
      //for(i=0;i<elmer_count;i++) if(data[i].used > 1) printf("%d - %d\n", data[i].used, i);
      return get_used(elmer_count);
}
